public class Test {
    /*
    #### BFS 解决最短路问题

1. [1926. 迷宫中离入口最近的出口](https://leetcode.cn/problems/nearest-exit-from-entrance-in-maze/)
class Solution {
    int[] dx = { 0, 0, 1, -1 };
    int[] dy = { 1, -1, 0, 0 };
    boolean[][] vis;
    int m, n;

    public int nearestExit(char[][] maze, int[] entrance) {
        m = maze.length;
        n = maze[0].length;
        vis = new boolean[m][n];

        int step = 0;

        Queue<int[]> q = new LinkedList<>();
        q.offer(entrance);
        vis[entrance[0]][entrance[1]] = true;
        while (!q.isEmpty()) {
            step++;
            int sz = q.size();
            for (int i = 0; i < sz; i++) {
                int[] t = q.poll();
                int a = t[0], b = t[1];
                vis[a][b] = true;
                for (int k = 0; k < 4; k++) {
                    int x = a + dx[k];
                    int y = b + dy[k];
                    if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' && !vis[x][y]) {
                        if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
                            return step;
                        }
                        q.offer(new int[] { x, y });
                        vis[x][y] = true;
                    }
                }
            }
        }

        return -1;
    }
}
2. [433. 最小基因变化](https://leetcode.cn/problems/minimum-genetic-mutation/)
class Solution {
    public int minMutation(String startGene, String endGene, String[] bank) {
        Set<String> vis =new HashSet<>(); //用来标记已经搜索过的状态
        Set<String> hash=new HashSet<>(); //用来统计基因库中的字符串
        for(String s:bank) hash.add(s);
        char[] change={'A','C','G','T'};
        if(startGene.equals(endGene)) return 0;
        if(!hash.contains(endGene)) return -1;
        Queue<String> q=new LinkedList<>();
        q.add(startGene);
        vis.add(startGene);
        int step=0;
        while(!q.isEmpty()){
            step++;
            int sz=q.size();
            while(sz--!=0){
                String t=q.poll();
                for(int i=0;i<8;i++){
                    char[] tmp=t.toCharArray();
                    for(int j=0;j<4;j++){
                        tmp[i]=change[j];
                        String next=new String(tmp);
                        if(hash.contains(next)&&!vis.contains(next)){
                            if(next.equals(endGene)) return step;
                            q.add(next);
                            vis.add(next);
                        }
                    }
                }
            }
        }
        return -1;
    }
}
3. [127. 单词接龙](https://leetcode.cn/problems/word-ladder/)
class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        Set<String> hash=new HashSet<>();
        for(String s:wordList) hash.add(s);
        Set<String> vis=new HashSet<>();
        if(!hash.contains(endWord)) return 0;
        Queue<String> q=new LinkedList<>();
        q.add(beginWord);
        vis.add(beginWord);
        int ret=1;
        while(!q.isEmpty()){
            ret++;
            int sz=q.size();
            while(sz--!=0){
                String t=q.poll();
                for(int i=0;i<t.length();i++){
                    char[] tmp=t.toCharArray();
                    for(char ch='a';ch<='z';ch++){
                        tmp[i]=ch;
                        String next=new String(tmp);
                        if(hash.contains(next)&&!vis.contains(next)){
                            if(next.equals(endWord)) return ret;
                            q.add(next);
                            vis.add(next);
                        }
                    }
                }
            }
        }
        return 0;
    }
}
4. [675. 为高尔夫比赛砍树](https://leetcode.cn/problems/cut-off-trees-for-golf-event/)
class Solution {
    int m , n;
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    boolean[][] vis ;
    public int cutOffTree(List<List<Integer>> f) {
        m = f.size();
        n = f.get(0).size();
        List<int[]> trees = new ArrayList<>();
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n ;j++) {
                if(f.get(i).get(j)> 1) {
                    trees.add(new int[]{i,j});//存入所有要砍树的点
                }
            }
        }
        //按从小到大的顺序
        Collections.sort(trees,(a,b) -> {
            return f.get(a[0]).get(a[1]) - f.get(b[0]).get(b[1]);
        });
        int bx = 0 , by = 0;
        int ret = 0;
        for(int[] tree : trees) {
            int x = tree[0];
            int y = tree[1];
            //记录从 bx,by 到 x , y 的最小距离
            int step = bfs(f,bx,by,x,y);
            if(step == -1) return -1;
            ret += step;
            //更新
            bx = x ;by = y;
        }
        return ret;
    }
    int bfs(List<List<Integer>> f,int bx, int by , int ex, int ey ) {
        if(bx == ex && by == ey) return 0;
        int step  = 0;
        vis = new boolean[m][n];

        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{bx,by});
        vis[bx][by] = true;

        while(!q.isEmpty()) {
            step++;
            int sz = q.size();
            while(sz-- !=0) {
                int[] t = q.poll();
                int a = t[0], b =t[1];
                vis[a][b] = true;
                for(int k = 0 ; k< 4;k++) {
                    int x = a + dx[k];
                    int y = b + dy[k];
                    if(x >=0 && x < m && y>=0 && y < n && !vis[x][y] && f.get(x).get(y) !=0) {
                        if(x == ex && y == ey) return step;
                        q.add(new int[]{x,y});
                        vis[x][y] = true;
                    }
                }
            }
        }

        return -1;
    }

}

     */
}
